let conuser = 'https://www.xuanheng99.com/api/v1';//域名
let conusers = 'https://www.xuanheng99.com';//域名
let app = getApp()
function connector() {
  // let conuser = 'https://www.xuanheng99.com/api/v1';//域名
  let urlPath = ''; //接口地址
  let imgPath = ''; //图片访问地址
  let filePath = '/file/token';//图片上传获取七牛token
  let fileDelete = '/file/delete';//图片删除
  let fileAuthPath = '/file/auth/token';//私有图片上传获取七牛token
  let fileAuthDelete = '/file/auth/delete';//私有图片删除
  let pageSize = 10;//每页显示条数
  let connector = {
    conuser: conuser,
    conusers:conusers,
    filePath: filePath,
    fileDelete: fileDelete,
    fileAuthPath:fileAuthPath,
    fileAuthDelete:fileAuthDelete,
    verify: conuser + "/token/verify",//验证token

    preOrder: conuser + "/pay/pre_order",//拉取微信支付
    
    orderPlace: conuser + "/order/place",//下单
    userAddress: conuser + "/user/address",//用户地址
    userAddr: conuser + "/user/addr",//获取用户地址

    orderList: conuser + "/order/list",//获取订单列表
    orderDetail: conuser + "/order/detail/",//获取订单详情 {order_sn}
    orderCancel: conuser + "/order/cancel",//取消订单

    goodsCategory: conuser + "/goods/category",//商品分类
    goodsList: conuser + "/goods/list/",//商品列表  {category_id}/{page}
    goodsDetail: conuser + "/goods/detail/",//商品详情 {id}
    goodsCollect: conuser + "/goods/collect/",//判断用户是否收藏商品{id}
    userGoods: conuser + "/user/goods",//收藏商品
    userList: conuser + "/user/list",//获取收藏商品列表

    coachAuth: conuser + "/coach/auth",//教练认证
    schoolList: conuser + "/school/list",//获取驾校
    userCode: conuser + "/user/code",//获取小程序码
    userIfo: conuser + "/user/info",//获取用户信息
    userWechat: conuser + "/user/wechat",//微信授权
    userFather: conuser + "/user/father",//创建父级
    userVipList: conuser + "/user/vip/list",//理赔进度
    captcha: conuser + "/captcha",//验证码接口
    userWithdraw: conuser + "/user/withdraw",//用户提现
    userFund: conuser + "/user/fund",//用户资金信息
    userMoneyList: conuser + "/user/money/list",//用户资金信息流水列表
    userCustomList: conuser + "/user/custom/list",//教练客户信息
    userMoneyRank: conuser + "/user/money/rank",//用户资金信息流水排名列表
    userMoneyLimit: conuser + "/user/money/limit",//提现金额限制

    
    vipList: conuser + "/vip/list/",//保险商品列表 {address_id}
    userSchool: conuser + "/user/school",//更新学校
    userRegion: conuser + "/user/region",//更新地区
    vipUserGet: conuser + "/vip/user/get",//用户VIP申请信息获取
    vipUserAdd: conuser + "/vip/user/add",//用户VIP申请信息添加
    vipUserEdit: conuser + "/vip/user/edit",//用户VIP申请信息编辑
    vipAgreement: conuser + "/vip/agreement/",//协议内容 {type}
    vipUserApply: conuser + "/vip/user/apply",//VIP保单信息申请
    vipUserStatus: conuser + "/vip/user/status",//用户VIP申请信息科目状态编辑
    vipUserDetail: conuser + "/vip/user/detail",//获取购买的VIP详情
    vipStatus: conuser + "/vip/status",//获取购买的VIP详情
    vipAddress: conuser + "/vip/address",//保险商品地区列表
    vipArea: conuser + "/vip/area/",//获取区县地区列表 {city}

    userCoachGet: conuser + "/user/coach/get",//获取教练信息接口
    userCoachEdit: conuser + "/user/coach/edit",//更新教练信息接口

    advertList: conuser + "/advert/list",//广告列表
    advertDetail: conuser + "/advert/detail",//广告详情
    
    subjectList: conuser + "/subject/list",//课程商品列表
    sundryList: conuser + "/sundry/list",//缴费专区
    sundryAdd: conuser + "/sundry/add",//用户缴费信息添加
    sundryOrderList: conuser + "/sundry/order/list",//用户缴费订单列表
    sundryOrderDetail: conuser + "/sundry/order/detail/",//用户订单详情 {order_sn}
  }
  return connector
}
module.exports.connector = connector;


/**
 * 数据请求
*/
function getFunc(requestUrl, getData, type, token, doneFunc, fileFunc) {
  // console.log(requestUrl);
  // console.log(getData);
  // console.log(type);
  wx.request({
    url: requestUrl,
    method: type,
    header: {
      token: token
    },
    data: getData,
    success: doneFunc,
    file:fileFunc
  })

}
module.exports.getFunc = getFunc;

//删除七牛云图片
function deleteFile(fileKey,token){
  console.log(fileKey)
  let obj = {
    fileName: fileKey
  }
  getFunc(connector().conuser + connector().fileDelete, obj, "post", token, function (res) {
    console.log(res)
  }, function (res) {
    console.log(res)
  })
}
module.exports.deleteFile = deleteFile;

//删除七牛云私有图片
function deleteFileAuth(fileKey,token){
  console.log(fileKey)
  let obj = {
    fileName: fileKey
  }
  getFunc(connector().conuser + connector().fileAuthDelete, obj, "post", token, function (res) {
    console.log(res)
  }, function (res) {
    console.log(res)
  })
}
module.exports.deleteFileAuth = deleteFileAuth;

//获取所有学校
function getFuncSchool(doneFunc, fileFunc) {
  console.log(getApp().tokens)
  wx.request({
    url: connector().schoolList,
    method: "get",
    header: {
      token: getApp().tokens
    },
    data: {},
    success: doneFunc,
    file:fileFunc
  })
}
module.exports.getFuncSchool = getFuncSchool;

//提示
function showToasts(str){
  wx.showToast({
    title: str,
    icon: 'none',
    duration: 1500//持续的时间
  })
}
module.exports.showToasts = showToasts;


//富文本解析
function convertHtmlToText(inputText) {
  console.log(inputText)
  var returnText = "" + inputText;
  returnText = returnText.replace(/<\/div>/ig, '\r\n');
  returnText = returnText.replace(/<\/li>/ig, '\r\n');
  returnText = returnText.replace(/<li>/ig, '  *  ');
  returnText = returnText.replace(/<\/ul>/ig, '\r\n');
  //-- remove BR tags and replace them with line break
  returnText = returnText.replace(/<br\s*[\/]?>/gi, "\r\n");

  //-- remove P and A tags but preserve what's inside of them
  returnText = returnText.replace(/<p.*?>/gi, "\r\n");
  returnText = returnText.replace(/<a.*href="(.*?)".*>(.*?)<\/a>/gi, " $2 ($1)");

  //-- remove all inside SCRIPT and STYLE tags
  returnText = returnText.replace(/<script.*>[\w\W]{1,}(.*?)[\w\W]{1,}<\/script>/gi, "");
  returnText = returnText.replace(/<style.*>[\w\W]{1,}(.*?)[\w\W]{1,}<\/style>/gi, "");
  //-- remove all else
  returnText = returnText.replace(/<(?:.|\s)*?>/g, "");

  //-- get rid of more than 2 multiple line breaks:
  returnText = returnText.replace(/(?:(?:\r\n|\r|\n)\s*){2,}/gim, "\r\n\r\n");

  //-- get rid of more than 2 spaces:
  returnText = returnText.replace(/ +(?= )/g, '');

  //-- get rid of html-encoded characters:
  returnText = returnText.replace(/ /gi, " ");
  returnText = returnText.replace(/&/gi, "&");
  returnText = returnText.replace(/"/gi, '"');
  returnText = returnText.replace(/</gi, '<');
  returnText = returnText.replace(/>/gi, '>');
  console.log(returnText)
  return returnText;

}
module.exports.convertHtmlToText = convertHtmlToText;
